package com.dmaximaging.fancypants {
	import flash.display.Sprite;
	import flash.net.URLRequest;
	import flash.display.Loader;
	
	import flash.geom.Rectangle;
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.display.StageScaleMode;
	import flash.display.StageAlign;
	import flash.events.Event;
	
	public class FancyItem extends Sprite{
		private var title:String;
		private var smallURL:String;
		private var largeURL:String;
		
		public var small_ldr:Loader;
		private var large_ldr:Loader;
		
		private var loadTrack_num:Number = 0;
		private var loadTarget_num:Number = 2;
		private var loaded_bool:Boolean = true;
		private var complete_fn:Function;
		
		private var persistWidth_num;
		private var persistHeight_num;
		
		public function FancyItem( item_xml:XML, complete_fn:Function ){
			title = item_xml.title;
			this.complete_fn = complete_fn;
			
			// Load the small image
			small_ldr = new Loader();
			small_ldr.contentLoaderInfo.addEventListener( Event.COMPLETE, loadComplete );
			small_ldr.load( new URLRequest( item_xml.smallURL ) );
			
			// Load the large image
			large_ldr = new Loader();
			large_ldr.contentLoaderInfo.addEventListener( Event.COMPLETE, loadComplete );
			large_ldr.load( new URLRequest( item_xml.largeURL ) );
		}
		
		private function loadComplete( event:Event ){
			if( ++loadTrack_num == loadTarget_num ){
				loaded_bool = true;
				renderSmall();
				complete_fn();
			}
		}
		
		private function renderSmall(){
			addChild( small_ldr );
			persistWidth_num = width;
			persistHeight_num = height;
			
			if( contains( large_ldr ) ){
				removeChild( large_ldr );
			}
			trace( "w,h: " + width + "," + height );
		}
		
		private function renderLarge(){
			addChild( large_ldr );
			width = persistWidth_num;
			height = persistHeight_num;			
			if( contains( small_ldr ) ){
				removeChild( small_ldr );
			}
		}
		
	}
}